﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using Model;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:CUST_MEMBERDAO
	/// </summary>
	public partial class CUST_MEMBERDAO
	{
		public CUST_MEMBERDAO()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ID", "CUST_MEMBER"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from CUST_MEMBER");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(CUST_MEMBER model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into CUST_MEMBER(");
			strSql.Append("MEMBERNO,NAME,GENDER,TYPE,MOBILE,BIRTHDAY,EMAIL,COUNTRY,PROVINCE,CITY,REGION,ADDRESS,ZIPCODE,MEM_LEVEL,AVA_POINTS,OVER_POINTS,STATUS,REMARK,CREATE_DATE,CREATE_USER,LAST_MODI_DATE,LAST_MODI_USER)");
			strSql.Append(" values (");
			strSql.Append("@MEMBERNO,@NAME,@GENDER,@TYPE,@MOBILE,@BIRTHDAY,@EMAIL,@COUNTRY,@PROVINCE,@CITY,@REGION,@ADDRESS,@ZIPCODE,@MEM_LEVEL,@AVA_POINTS,@OVER_POINTS,@STATUS,@REMARK,@CREATE_DATE,@CREATE_USER,@LAST_MODI_DATE,@LAST_MODI_USER)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@MEMBERNO", SqlDbType.VarChar,20),
					new SqlParameter("@NAME", SqlDbType.VarChar,50),
					new SqlParameter("@GENDER", SqlDbType.Bit,1),
					new SqlParameter("@TYPE", SqlDbType.Int,4),
					new SqlParameter("@MOBILE", SqlDbType.VarChar,20),
					new SqlParameter("@BIRTHDAY", SqlDbType.DateTime),
					new SqlParameter("@EMAIL", SqlDbType.VarChar,50),
					new SqlParameter("@COUNTRY", SqlDbType.VarChar,50),
					new SqlParameter("@PROVINCE", SqlDbType.VarChar,20),
					new SqlParameter("@CITY", SqlDbType.NVarChar,20),
					new SqlParameter("@REGION", SqlDbType.VarChar,50),
					new SqlParameter("@ADDRESS", SqlDbType.NVarChar,128),
					new SqlParameter("@ZIPCODE", SqlDbType.VarChar,20),
					new SqlParameter("@MEM_LEVEL", SqlDbType.VarChar,20),
					new SqlParameter("@AVA_POINTS", SqlDbType.Int,4),
					new SqlParameter("@OVER_POINTS", SqlDbType.Int,4),
					new SqlParameter("@STATUS", SqlDbType.Int,4),
					new SqlParameter("@REMARK", SqlDbType.VarChar,500),
					new SqlParameter("@CREATE_DATE", SqlDbType.DateTime),
					new SqlParameter("@CREATE_USER", SqlDbType.VarChar,20),
					new SqlParameter("@LAST_MODI_DATE", SqlDbType.DateTime),
					new SqlParameter("@LAST_MODI_USER", SqlDbType.VarChar,20)};
			parameters[0].Value = model.MEMBERNO;
			parameters[1].Value = model.NAME;
			parameters[2].Value = model.GENDER;
			parameters[3].Value = model.TYPE;
			parameters[4].Value = model.MOBILE;
			parameters[5].Value = model.BIRTHDAY;
			parameters[6].Value = model.EMAIL;
			parameters[7].Value = model.COUNTRY;
			parameters[8].Value = model.PROVINCE;
			parameters[9].Value = model.CITY;
			parameters[10].Value = model.REGION;
			parameters[11].Value = model.ADDRESS;
			parameters[12].Value = model.ZIPCODE;
			parameters[13].Value = model.MEM_LEVEL;
			parameters[14].Value = model.AVA_POINTS;
			parameters[15].Value = model.OVER_POINTS;
			parameters[16].Value = model.STATUS;
			parameters[17].Value = model.REMARK;
			parameters[18].Value = model.CREATE_DATE;
			parameters[19].Value = model.CREATE_USER;
			parameters[20].Value = model.LAST_MODI_DATE;
			parameters[21].Value = model.LAST_MODI_USER;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(CUST_MEMBER model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update CUST_MEMBER set ");
			strSql.Append("MEMBERNO=@MEMBERNO,");
			strSql.Append("NAME=@NAME,");
			strSql.Append("GENDER=@GENDER,");
			strSql.Append("TYPE=@TYPE,");
			strSql.Append("MOBILE=@MOBILE,");
			strSql.Append("BIRTHDAY=@BIRTHDAY,");
			strSql.Append("EMAIL=@EMAIL,");
			strSql.Append("COUNTRY=@COUNTRY,");
			strSql.Append("PROVINCE=@PROVINCE,");
			strSql.Append("CITY=@CITY,");
			strSql.Append("REGION=@REGION,");
			strSql.Append("ADDRESS=@ADDRESS,");
			strSql.Append("ZIPCODE=@ZIPCODE,");
			strSql.Append("MEM_LEVEL=@MEM_LEVEL,");
			strSql.Append("AVA_POINTS=@AVA_POINTS,");
			strSql.Append("OVER_POINTS=@OVER_POINTS,");
			strSql.Append("STATUS=@STATUS,");
			strSql.Append("REMARK=@REMARK,");
			strSql.Append("CREATE_DATE=@CREATE_DATE,");
			strSql.Append("CREATE_USER=@CREATE_USER,");
			strSql.Append("LAST_MODI_DATE=@LAST_MODI_DATE,");
			strSql.Append("LAST_MODI_USER=@LAST_MODI_USER");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@MEMBERNO", SqlDbType.VarChar,20),
					new SqlParameter("@NAME", SqlDbType.VarChar,50),
					new SqlParameter("@GENDER", SqlDbType.Bit,1),
					new SqlParameter("@TYPE", SqlDbType.Int,4),
					new SqlParameter("@MOBILE", SqlDbType.VarChar,20),
					new SqlParameter("@BIRTHDAY", SqlDbType.DateTime),
					new SqlParameter("@EMAIL", SqlDbType.VarChar,50),
					new SqlParameter("@COUNTRY", SqlDbType.VarChar,50),
					new SqlParameter("@PROVINCE", SqlDbType.VarChar,20),
					new SqlParameter("@CITY", SqlDbType.NVarChar,20),
					new SqlParameter("@REGION", SqlDbType.VarChar,50),
					new SqlParameter("@ADDRESS", SqlDbType.NVarChar,128),
					new SqlParameter("@ZIPCODE", SqlDbType.VarChar,20),
					new SqlParameter("@MEM_LEVEL", SqlDbType.VarChar,20),
					new SqlParameter("@AVA_POINTS", SqlDbType.Int,4),
					new SqlParameter("@OVER_POINTS", SqlDbType.Int,4),
					new SqlParameter("@STATUS", SqlDbType.Int,4),
					new SqlParameter("@REMARK", SqlDbType.VarChar,500),
					new SqlParameter("@CREATE_DATE", SqlDbType.DateTime),
					new SqlParameter("@CREATE_USER", SqlDbType.VarChar,20),
					new SqlParameter("@LAST_MODI_DATE", SqlDbType.DateTime),
					new SqlParameter("@LAST_MODI_USER", SqlDbType.VarChar,20),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = model.MEMBERNO;
			parameters[1].Value = model.NAME;
			parameters[2].Value = model.GENDER;
			parameters[3].Value = model.TYPE;
			parameters[4].Value = model.MOBILE;
			parameters[5].Value = model.BIRTHDAY;
			parameters[6].Value = model.EMAIL;
			parameters[7].Value = model.COUNTRY;
			parameters[8].Value = model.PROVINCE;
			parameters[9].Value = model.CITY;
			parameters[10].Value = model.REGION;
			parameters[11].Value = model.ADDRESS;
			parameters[12].Value = model.ZIPCODE;
			parameters[13].Value = model.MEM_LEVEL;
			parameters[14].Value = model.AVA_POINTS;
			parameters[15].Value = model.OVER_POINTS;
			parameters[16].Value = model.STATUS;
			parameters[17].Value = model.REMARK;
			parameters[18].Value = model.CREATE_DATE;
			parameters[19].Value = model.CREATE_USER;
			parameters[20].Value = model.LAST_MODI_DATE;
			parameters[21].Value = model.LAST_MODI_USER;
			parameters[22].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from CUST_MEMBER ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from CUST_MEMBER ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public CUST_MEMBER GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,MEMBERNO,NAME,GENDER,TYPE,MOBILE,BIRTHDAY,EMAIL,COUNTRY,PROVINCE,CITY,REGION,ADDRESS,ZIPCODE,MEM_LEVEL,AVA_POINTS,OVER_POINTS,STATUS,REMARK,CREATE_DATE,CREATE_USER,LAST_MODI_DATE,LAST_MODI_USER from CUST_MEMBER ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			CUST_MEMBER model=new CUST_MEMBER();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public CUST_MEMBER DataRowToModel(DataRow row)
		{
			CUST_MEMBER model=new CUST_MEMBER();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["MEMBERNO"]!=null)
				{
					model.MEMBERNO=row["MEMBERNO"].ToString();
				}
				if(row["NAME"]!=null)
				{
					model.NAME=row["NAME"].ToString();
				}
				if(row["GENDER"]!=null && row["GENDER"].ToString()!="")
				{
					if((row["GENDER"].ToString()=="1")||(row["GENDER"].ToString().ToLower()=="true"))
					{
						model.GENDER=true;
					}
					else
					{
						model.GENDER=false;
					}
				}
				if(row["TYPE"]!=null && row["TYPE"].ToString()!="")
				{
					model.TYPE=int.Parse(row["TYPE"].ToString());
				}
				if(row["MOBILE"]!=null)
				{
					model.MOBILE=row["MOBILE"].ToString();
				}
				if(row["BIRTHDAY"]!=null && row["BIRTHDAY"].ToString()!="")
				{
					model.BIRTHDAY=DateTime.Parse(row["BIRTHDAY"].ToString());
				}
				if(row["EMAIL"]!=null)
				{
					model.EMAIL=row["EMAIL"].ToString();
				}
				if(row["COUNTRY"]!=null)
				{
					model.COUNTRY=row["COUNTRY"].ToString();
				}
				if(row["PROVINCE"]!=null)
				{
					model.PROVINCE=row["PROVINCE"].ToString();
				}
				if(row["CITY"]!=null)
				{
					model.CITY=row["CITY"].ToString();
				}
				if(row["REGION"]!=null)
				{
					model.REGION=row["REGION"].ToString();
				}
				if(row["ADDRESS"]!=null)
				{
					model.ADDRESS=row["ADDRESS"].ToString();
				}
				if(row["ZIPCODE"]!=null)
				{
					model.ZIPCODE=row["ZIPCODE"].ToString();
				}
				if(row["MEM_LEVEL"]!=null)
				{
					model.MEM_LEVEL=row["MEM_LEVEL"].ToString();
				}
				if(row["AVA_POINTS"]!=null && row["AVA_POINTS"].ToString()!="")
				{
					model.AVA_POINTS=int.Parse(row["AVA_POINTS"].ToString());
				}
				if(row["OVER_POINTS"]!=null && row["OVER_POINTS"].ToString()!="")
				{
					model.OVER_POINTS=int.Parse(row["OVER_POINTS"].ToString());
				}
				if(row["STATUS"]!=null && row["STATUS"].ToString()!="")
				{
					model.STATUS=int.Parse(row["STATUS"].ToString());
				}
				if(row["REMARK"]!=null)
				{
					model.REMARK=row["REMARK"].ToString();
				}
				if(row["CREATE_DATE"]!=null && row["CREATE_DATE"].ToString()!="")
				{
					model.CREATE_DATE=DateTime.Parse(row["CREATE_DATE"].ToString());
				}
				if(row["CREATE_USER"]!=null)
				{
					model.CREATE_USER=row["CREATE_USER"].ToString();
				}
				if(row["LAST_MODI_DATE"]!=null && row["LAST_MODI_DATE"].ToString()!="")
				{
					model.LAST_MODI_DATE=DateTime.Parse(row["LAST_MODI_DATE"].ToString());
				}
				if(row["LAST_MODI_USER"]!=null)
				{
					model.LAST_MODI_USER=row["LAST_MODI_USER"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,MEMBERNO,NAME,GENDER,TYPE,MOBILE,BIRTHDAY,EMAIL,COUNTRY,PROVINCE,CITY,REGION,ADDRESS,ZIPCODE,MEM_LEVEL,AVA_POINTS,OVER_POINTS,STATUS,REMARK,CREATE_DATE,CREATE_USER,LAST_MODI_DATE,LAST_MODI_USER ");
			strSql.Append(" FROM CUST_MEMBER ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,MEMBERNO,NAME,GENDER,TYPE,MOBILE,BIRTHDAY,EMAIL,COUNTRY,PROVINCE,CITY,REGION,ADDRESS,ZIPCODE,MEM_LEVEL,AVA_POINTS,OVER_POINTS,STATUS,REMARK,CREATE_DATE,CREATE_USER,LAST_MODI_DATE,LAST_MODI_USER ");
			strSql.Append(" FROM CUST_MEMBER ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM CUST_MEMBER ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from CUST_MEMBER T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "CUST_MEMBER";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

